/**
 * Created by liumin on 14-7-14.
 */


/**
 * Serializer
 * 数据源
 */

if(typeof (module) !="undefined"){
    module.exports = FColorConverter; //
}




function FColorConverter()
{

}

// cmyk小数掉后保留的位数
FColorConverter.prototype.CMYK_FIXED_NUMBER= 2;

//----------------------------基础功能----------------------------------------
// RGB 	-> CMY
//RGB values from 0 to 255
//CMY values  from 0.00 to 1.00
FColorConverter.prototype.RGB_2_CMY=function(R, G, B){
    var C = 1 - ( R*1.0/255 );
    var M = 1 - ( G*1.0/255 );
    var Y = 1 - ( B*1.0/255 );

    var ret = new Array();
    ret.push((C.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)),
        (M.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)),
        (Y.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)));
    return ret;
}
// CMY 	-> CMYK
// CMYK and CMY values from 0.00 to 1.00
FColorConverter.prototype.CMY_2_CMYK=function(C, M, Y){
    var var_K  = 1.0;

    if ( C < var_K )   var_K = C;
    if ( M < var_K )   var_K = M;
    if ( Y < var_K )   var_K = Y;
    if ( var_K == 1 ) { //Black
        C = 0;
        M = 0;
        Y = 0;
    }else {
        C = ( C - var_K ) / ( 1 - var_K );
        M = ( M - var_K ) / ( 1 - var_K );
        Y = ( Y - var_K ) / ( 1 - var_K );
    }
    var K = var_K;

    var ret = new Array();
    ret.push((C.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)),
        (M.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)),
        (Y.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)),
        (K.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)));
    return ret;
}

// CMYK -> CMY
// CMYK and CMY values from 0.00 to 1.00
FColorConverter.prototype.CMYK_2_CMY=function(C, M, Y, K){
    C = ( C * ( 1 - K ) + K );
    M = ( M * ( 1 - K ) + K );
    Y = ( Y * ( 1 - K ) + K );

    var ret = new Array();
    ret.push((C.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)),
        (M.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)),
        (Y.toFixed(FColorConverter.prototype.CMYK_FIXED_NUMBER)));
    return ret;
}
// CMY 	-> RGB
//RGB values from 0 to 255
//CMY values  from 0.00 to 1.00
FColorConverter.prototype.CMY_2_RGB=function(C, M, Y){
    var R = ( 1 - C ) * 255;
    var G = ( 1 - M ) * 255;
    var B = ( 1 - Y ) * 255;

    var ret = new Array();
    ret.push(R,G,B);
    return ret;
}
//----------------------------扩展函数----------------------------------------
// CMYK -> RGB
//CMYK values  from 0.00 to 1.00
//RGB values from 0 to 255
FColorConverter.prototype.CMYK_2_RGB=function(C, M, Y, K){
    C *=1.0;
    M *=1.0;
    Y *=1.0;
    K *=1.0;
    var cmy = FColorConverter.prototype.CMYK_2_CMY(C, M, Y, K);
    var rgb = FColorConverter.prototype.CMY_2_RGB(cmy[0], cmy[1], cmy[2]);
    return rgb;
}
// RGB -> CMYK
//RGB values from 0 to 255
//CMYK values  from 0.00 to 1.00
FColorConverter.prototype.RGB_2_CMYK=function(R, G, B){
    R *=1.0;
    G *=1.0;
    B *=1.0;
    var cmy = FColorConverter.prototype.RGB_2_CMY(R,G,B);
    var cmyk = FColorConverter.prototype.CMY_2_CMYK(cmy[0], cmy[1], cmy[2]);
    return cmyk;
}


